package org.jeecg.modules.api.controller.daping;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.DictUtil;
import org.jeecg.modules.api.controller.ShippingVo;
import org.jeecg.modules.api.controller.daping.mapper.DaPingMapper;
import org.jeecg.modules.api.controller.daping.vo.HyDaPingVO;
import org.jeecg.modules.api.wlhy.app.bean.ShippingNoteInfo;
import org.jeecg.modules.wlhy.customer.service.IHyCustomerService;
import org.jeecg.modules.wlhy.hydriver.service.IHyDriverService;
import org.jeecg.modules.wlhy.hyvehicle.service.IHyVehicleService;
import org.jeecg.modules.wlhy.order.entity.HyOrder;
import org.jeecg.modules.wlhy.order.service.IHyOrderService;
import org.jeecg.modules.wlhy.setting.service.IHySettingService;
import org.jeecg.modules.wlhy.shipping.entity.HyShippingNote;
import org.jeecg.modules.wlhy.shipping.service.IHyShippingNoteService;
import org.jeecg.modules.wlhy.tj.mapper.HyTjMapper;
import org.jeecg.modules.wlhy.tj.vo.HyOrderTj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author scott
 * @since 2018-12-17
 */
@RestController
@RequestMapping("/api/wlhy/dapingCw")
@Api(tags = "大屏")
@Slf4j
public class DaPingCaiWuController {

    @Autowired
    private DaPingMapper daPingMapper;


    @Autowired
    private IHyShippingNoteService hyShippingNoteService;

    @Autowired
    private IHyOrderService hyOrderService;

    @Autowired
    private DictUtil dictUtil;

    /**
     * 总成交金额
     *
     * @return
     */
    @GetMapping(value = "/getzcjje")
    public String getzcjje() {


        List<String> resultList = new ArrayList<String>();
        Calendar cal = Calendar.getInstance();
        //近7个月
        cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) + 1); //要先+1,才能把本月的算进去
        for (int i = 0; i < 7; i++) {
            cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) - 1); //逐次往前推1个月
            resultList.add(String.valueOf(cal.get(Calendar.YEAR))
                    + "-" + (cal.get(Calendar.MONTH) + 1 < 10 ? "0" +
                    (cal.get(Calendar.MONTH) + 1) : (cal.get(Calendar.MONTH) + 1)));
        }
        JSONArray list = new JSONArray();
        for (String s : resultList) {
            JSONObject sjres = new JSONObject();
            HyDaPingVO getzcjje = daPingMapper.getzcjje(s);
            if(getzcjje!=null){
                sjres.put("name", getzcjje.getName());
                sjres.put("value", getzcjje.getValue() == null ? '0' : getzcjje.getValue());
                sjres.put("type", "金额");
                list.add(sjres);
            }

        }

        return list.toJSONString();
    }

    /**
     * 总成交订单
     *
     * @return
     */
    @GetMapping(value = "/getzcjdd")
    public String getzcjdd() {

        List<String> resultList = new ArrayList<String>();
        Calendar cal = Calendar.getInstance();
        //近7个月
        cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) + 1); //要先+1,才能把本月的算进去
        for (int i = 0; i < 7; i++) {
            cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) - 1); //逐次往前推1个月
            resultList.add(String.valueOf(cal.get(Calendar.YEAR))
                    + "-" + (cal.get(Calendar.MONTH) + 1 < 10 ? "0" +
                    (cal.get(Calendar.MONTH) + 1) : (cal.get(Calendar.MONTH) + 1)));
        }
        JSONArray list = new JSONArray();
        for (String s : resultList) {
            JSONObject sjres = new JSONObject();
            HyDaPingVO getzcjje = daPingMapper.getzcjdd(s);
            if(getzcjje!=null){
                if(getzcjje.getName()!=null){
                    sjres.put("name", getzcjje.getName());
                    sjres.put("value", getzcjje.getValue() == null ? '0' : getzcjje.getValue());
                    sjres.put("type", "单");
                    list.add(sjres);
                }

            }

        }
        return list.toJSONString();
    }


    /**
     * 总成交订单
     *
     * @return
     */
    @GetMapping(value = "/getzje")
    public String getzje() {

        JSONObject sjres = new JSONObject();
        String zje = daPingMapper.getzje();
        sjres.put("value", zje);
        return sjres.toJSONString();
    }

    /**
     * 总成交运单数颜色块
     *
     * @return
     */
    @GetMapping(value = "/getgzsysk")
    public String getgzsysk() {

        int cjjes = hyShippingNoteService.count(new QueryWrapper<HyShippingNote>().eq("del_flag","0"));
        int cjjdd = hyOrderService.count(new QueryWrapper<HyOrder>().eq("del_flag","0"));
        int cjjhy = hyOrderService.count(new QueryWrapper<HyOrder>().eq("del_flag","0"));
        int zfds = hyShippingNoteService.count(new QueryWrapper<HyShippingNote>().eq("shipping_pay_status","6"));
        JSONArray list = new JSONArray();
        JSONObject sjres = new JSONObject();
        sjres.put("backgroundColor", "#409EFF");
        sjres.put("prefixText", "总成交运单数");
        sjres.put("value", cjjes);
        sjres.put("suffixText", "");
        list.add(sjres);

        JSONObject sjres1 = new JSONObject();
        sjres1.put("backgroundColor", "#409EFF");
        sjres1.put("prefixText", "总成交订单数");
        sjres1.put("value", cjjdd);
        sjres1.put("suffixText", "");
        list.add(sjres1);

        JSONObject sjres2 = new JSONObject();
        sjres2.put("backgroundColor", "#409EFF");
        sjres2.put("prefixText", "总成交货源数");
        sjres2.put("value", cjjhy);
        sjres2.put("suffixText", "");
        list.add(sjres2);

        JSONObject sjres3 = new JSONObject();
        sjres3.put("backgroundColor", "#409EFF");
        sjres3.put("prefixText", "支付单数");
        sjres3.put("value", zfds);
        sjres3.put("suffixText", "");
        list.add(sjres3);


        return list.toJSONString();
    }


    /**
     * 近7日调度量
     *
     * @return
     */
    @GetMapping(value = "/getddl")
    public String getddl() {

        List<String> resultList = new ArrayList<String>();
//        Calendar cal = Calendar.getInstance();
        Calendar c = null;
        //近7个月
//        cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.MONTH) + 1); //要先+1,才能把本月的算进去
        for (int i = 0; i < 7; i++) {
//            cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) - 1); //逐次往前推1个月
            c=Calendar.getInstance();
            c.add(Calendar.DAY_OF_MONTH, -i);
            resultList.add(c.get(Calendar.YEAR)
                    + "-" + (c.get(Calendar.MONTH) + 1 < 10 ? "0" +
                    (c.get(Calendar.MONTH) + 1) : (c.get(Calendar.MONTH) + 1))+"-"+c.get(Calendar.DAY_OF_MONTH));
        }
        JSONArray list = new JSONArray();
        for (String s : resultList) {
            JSONObject sjres = new JSONObject();
            HyDaPingVO getzcjje = daPingMapper.getzcjds(s);
            if(getzcjje!=null){
                sjres.put("name", getzcjje.getName());
                sjres.put("value", getzcjje.getValue() == null ? '0' : getzcjje.getValue());
                sjres.put("type", "吨");
                list.add(sjres);
            }

        }

        return list.toJSONString();
    }



    /**
     * 承运订单概览
     *
     * @return
     */
    @GetMapping(value = "/getcyddgl")
    public String getcyddgl() {


        JSONArray list = new JSONArray();
        int wanjie = hyOrderService.count(new QueryWrapper<HyOrder>().eq("order_status","8"));

        int chexiao = hyOrderService.count(new QueryWrapper<HyOrder>().eq("del_flag","-1"));
        int diaodu = hyOrderService.count(new QueryWrapper<HyOrder>().eq("order_status","2"));
        int daishouli = hyOrderService.count(new QueryWrapper<HyOrder>().eq("order_status","0"));
        JSONObject sjres = new JSONObject();
        sjres.put("name", "已完结");
        sjres.put("value", wanjie);
        list.add(sjres);
        JSONObject sjres1 = new JSONObject();
        sjres1.put("name", "已撤销");
        sjres1.put("value", chexiao);
        list.add(sjres1);
        JSONObject sjres2 = new JSONObject();
        sjres2.put("name", "调度中");
        sjres2.put("value", diaodu);
        list.add(sjres2);
        JSONObject sjres3 = new JSONObject();
        sjres3.put("name", "待受理");
        sjres3.put("value", daishouli);
        list.add(sjres3);

        return list.toJSONString();
    }


    @GetMapping(value = "/getqsyundan")
    public Object[] getqsyundan() {
        String strDateFormat = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat f = new SimpleDateFormat(strDateFormat);

        List<HyShippingNote> listJd = hyShippingNoteService.list(new QueryWrapper<HyShippingNote>()
                .eq("shipping_status", "5")
        );
        List<String[]> list2 = new ArrayList<>();
        for (int i = 0; i < listJd.size(); i++) {
            String[] obj = new String[3];
            JSONObject jsonObject = dictUtil.parseObjectDict(listJd.get(i));
            obj[0] = jsonObject.get("vehicleId_dictText").toString();
            obj[1] = jsonObject.get("driverId_dictText").toString();
            obj[2] = f.format(listJd.get(i).getCreateTime());
            list2.add(obj);
        }
        Object[] objects = list2.toArray();
        return objects;
    }


}
